package cl.whyem.springoxm.test.util;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;

public class MarshallerUtil {

    public static <T> String toXML(Marshaller marshaller, T object) throws IOException {
        StringWriter out = new StringWriter();
        marshaller.marshal(object, new StreamResult(out));
        return out.toString();
    }

    public static <T> T fromXML(Unmarshaller marshaller, String xml) throws IOException {
        return (T) marshaller.unmarshal(new StreamSource(new StringReader(xml)));
    }

}
